package ro.xblue.translator;

import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.servlet.Constants;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.HttpUtils;
import ro.gateway.aida.utils.Utils;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: EditTransItemAction.java,v 1.1 2004/10/24 23:37:03 mihaipostelnicu Exp $
 */
public class EditTransItemAction extends Action
{
      public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception
      {
            User logged_user = UserUtils.getLogged( request );
            PersistenceToken token = PersistenceManager.tokenLookup( getServlet().getServletContext() );
            if ( logged_user == null || ( !Arbiter.getManager( token ).canDo( request, Perms.ADMIN_TOOLS ) ) )
            {
                  return mapping.findForward( "norights" );
            }
			Utils.setBreadCrumbs(request,"bc.edittag");	
            ServletContext application = getServlet().getServletContext();
            Hashtable services = ( Hashtable ) application.
                  getAttribute( Constants.SERVICES );
            Translator_DB translator_db = ( Translator_DB ) application.
                  getAttribute( TranslatorConstants.DB_TRANSLATOR );

            TransItemForm item_form = ( TransItemForm ) form;

            String action = request.getParameter( "action" );
            if ( action == null ) action = "edit";
            long id = HttpUtils.getLong( request, "id", -1 );

            String src_lang = HttpUtils.getValidTrimedString( request, "src_lang", null );
            String dest_lang = HttpUtils.getValidTrimedString( request, "dest_lang", null );
            if ( ( "edit".equals( action ) ) && ( ( src_lang != null ) || ( dest_lang != null ) ) )
            {
                  item_form.setReferer(HttpUtils.getReferer(request, null, null, null));
                  if ( ( src_lang != null ) && ( !src_lang.equals( item_form.getSrc_lang() ) ) )
                  {
                        item_form.setSrc_lang( src_lang );
//                        System.out.println( "new src lang:" + src_lang );
                  }
                  if ( ( dest_lang != null ) && ( !dest_lang.equals( item_form.getDest_lang() ) ) )
                  {
                        item_form.setDest_lang( dest_lang );
//                        System.out.println( "new dest lang:" + dest_lang );
                  }
                  request.setAttribute( "all_langs_items", item_form.getAll_langs_items() );
                  return mapping.findForward( "edit" );
            }

//            System.out.println( "action:" + action );
            if ( "new".equals( action ) )
            {
                  item_form.setReferer(HttpUtils.getReferer(request, null, null, null));
                  item_form.reset_defaults();
                  if ( dest_lang != null )
                  {
                        item_form.setDest_lang( dest_lang );
                  }
                  item_form.setLabel( HttpUtils.getValidTrimedString(
                        request, "label", null ) );
                  item_form.setModule( HttpUtils.getValidTrimedString(
                        request, "module", null ) );
//                  System.out.println( item_form.getId() );
                  return mapping.findForward( "edit" );
            }
            else if ( "edit".equals( action ) )
            {
                  item_form.setReferer(HttpUtils.getReferer(request, null, null, null));
                  if ( id == -1 )
                  {
                        return mapping.findForward( "finnish" );
                  }
                  TransItem item = null;
                  TransItem[] all_langs_items = null;
                  try
                  {
                        item = TranslatorDBAccessor.getManager( token ).
                              getById( id );
                        if ( item != null )
                        {
//                              System.out.println( "found tr_item: " + id );
                              all_langs_items = TranslatorDBAccessor.getManager( token ).
                                    getByModuleAndLabel( item.getModule(), item.getLabel() );
                        }
                        else
                        {
//                              System.out.println( "tr_item not found: " + id );
                        }
                  }
                  catch ( SQLException sqlex )
                  {
                        sqlex.printStackTrace();
                  }
                  if ( item == null ) return mapping.findForward( "finnish" );

                  item_form.populate( item, all_langs_items );
                  if ( all_langs_items != null )
                  {
                        request.setAttribute( "all_langs_items", all_langs_items );
                        item_form.setSrc_lang( all_langs_items[ 0 ].getLang() );
                  }

                  return mapping.findForward( "edit" );
            }
            else if ( "save".equals( action ) )
            {
                  item_form.setModule( HttpUtils.
                        getValidTrimedString( request, "module", null ) );
                  item_form.setText( HttpUtils.
                        getValidTrimedString( request, "text", null ) );
                  item_form.setLabel( HttpUtils.
                        getValidTrimedString( request, "label", null ) );

                  String[] errors = item_form.isValid();
                  if ( errors != null )
                  {
                        request.setAttribute( "errors", errors );
                        request.setAttribute( "all_langs_items", item_form.getAll_langs_items() );
                        return mapping.findForward( "edit" );
                  }

                  try
                  {
                        if ( item_form.dest_item.id != -1 )
                        {
                              TranslatorDBAccessor.getManager( token ).updateText(
                                    item_form.dest_item );
                        }
                        else
                        {
                              TranslatorDBAccessor.getManager( token ).insert( item_form.dest_item );
                        }
//                        System.out.println( "reloading:" + item_form.getModule() );
                        translator_db.reloadModule( item_form.getModule() );
                  }
                  catch ( Exception ex )
                  {
                        ex.printStackTrace();
                  }
            }

            if (item_form.referer != null) {
                  response.sendRedirect(item_form.referer);
                  return null;
            }
            return mapping.findForward( "finnish" );
      }
}
